package com.zhaogang.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhaogang.dao.SaleAccountMapper;
import com.zhaogang.dao.SaleDetailsMapper;
import com.zhaogang.dao.SaleSettleinfoMapper;
import com.zhaogang.entity.SaleAccount;
import com.zhaogang.entity.SaleDetails;
import com.zhaogang.entity.SaleSettleinfo;
import com.zhaogang.service.SaleInfoService;

/**
* @author
* @data
* @version
* @parameter
* @return
*/
@Service
public class SaleInfoServiceImpl implements SaleInfoService {
	
	@Autowired
	private SaleAccountMapper saleAccountMapper;
	@Autowired
	private SaleDetailsMapper saleDetailsMapper;
	@Autowired
	private SaleSettleinfoMapper saleSettleInfoMapper;
	

	public int validateAccount(String account) throws Exception{
		// TODO Auto-generated method stub
		int result=3;
		SaleAccount saleAccount=new SaleAccount();
		
		if(account.length()<6&&account.length()>30)
		{
			result=1;
		}else if(!account.matches("[a-zA-Z0-9_]{6,30}"))
		{
			result=2;
		}else 
		{
			saleAccount=saleAccountMapper.selectByAccount(account);
			if((saleAccount!=null)){
				result=0;
			}
		}
		
		return result;
	}

	public Map<String, Object> selectSaleInfo(String Account)throws Exception {
		// TODO Auto-generated method stub
		SaleAccount saleAccount=new SaleAccount();

		Map<String,Object> map=new HashMap<String, Object>();
		//基本信息
		saleAccount=saleAccountMapper.selectByAccount(Account);
		System.out.println("saleAccount:"+saleAccount);
		if(saleAccount!=null)
		{
			map.put("saleAccount", saleAccount);
			//详细信息
			int accountId=saleAccount.getId();
			SaleDetails details=new SaleDetails();
			SaleDetails resultSaleDetails=new SaleDetails();
			details.setSaleAccountId(accountId);
			resultSaleDetails=saleDetailsMapper.selectDetailsSelective(details);
			System.out.println("resultSaleDetails:"+resultSaleDetails);
			if(resultSaleDetails!=null){
				map.put("resultSaleDetails", resultSaleDetails);
				}
				//注册信息
				SaleSettleinfo settleinfo=new SaleSettleinfo();
				SaleSettleinfo resultSettleinfo=new SaleSettleinfo();
				settleinfo.setSaleAccountId(accountId);
				resultSettleinfo=saleSettleInfoMapper.selectInfoSelective(settleinfo);
				System.out.println("resultSettleinfo:"+resultSettleinfo);
			if(resultSettleinfo!=null){
					map.put("resultSettleinfo", resultSettleinfo);	
				}
			
		}
		
		return map;
	}

	public int updateAccountPW(List<String> list)throws Exception {
		// TODO Auto-generated method stub
		int result=0;
		String newpsw=list.get(0);
		String oldpsw=list.get(1);
		String account=list.get(2);
		SaleAccount saleAccount =new SaleAccount();
		saleAccount=saleAccountMapper.selectByAccount(account);
		if(saleAccount!=null){
			if(oldpsw.equals(saleAccount.getPassword()))
			{
				result=1;
				saleAccount.setPassword(newpsw);;
				saleAccountMapper.updateByPrimaryKeySelective(saleAccount);
			}else {
				result=2;
			}
		}
			
		
		return result;
	}

	public void updateSaleInfo(List<Object> saleInfo)throws Exception{
		// TODO Auto-generated method stub
		int i=0;
		int j=0;
		for( Object item:saleInfo){
			if(item instanceof SaleAccount){	
				i=saleAccountMapper.updateByPrimaryKeySelective((SaleAccount)item);
			}else if(item instanceof SaleDetails){
				SaleDetails record=new SaleDetails();
				//将item中的saleAccountId传给一个新的SaleDetails对象，然后进行条件查询，否则查询失败
				record.setSaleAccountId(((SaleDetails)item).getSaleAccountId());
				SaleDetails result=saleDetailsMapper.selectDetailsSelective(record);
				if(result!=null){
					j=saleDetailsMapper.updateByAccountId((SaleDetails)item);
				}else{
					j=saleDetailsMapper.insertSelective((SaleDetails)item);
				}
				
			}
	}
		System.out.println("受影响行数分别为：i:"+i+",j:"+j);
	}

	
}
